define(["jquery", "ec", "core/util","b64"], function($, echarts, util,B64) {
	
	var _icon=null,
		_PluginObject=null,
		status=[{
			value:0,
			text:"未处理"
		},{
			value:1,
			text:"正在处理"
		},{
			value:2,
			text:"跟进"
		},{
			value:3,
			text:"驳回"
		},{
			value:4,
			text:"已解决"
		}];

	var View = {

		initView: function(_this) {
			var d2=$("#ticket-grid"),
				form=$("#ticket-form"),
				jobId=$("#jobId").val();
				
			d2.datagrid({
				fitColumns:true,
				url:"/ticket/query/"+$("#jobId").val(),
				method:"get",
				idField:"UUID",
				singleSelect: true,
				view: detailview,
                detailFormatter:function(index,row){
                    return '<div class="ddv" style="padding:5px 0"></div>';
                },
                onExpandRow: function(index,row){
                	var d=d2.datagrid('getRows')[index];
                    var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
                    ddv.panel({
                        height:500,
                        border:false,
                        cache:false,
                        content:"<iframe src='/ticket/tail/"+d.UUID+"'width='100%' height='96%'></iframe>",
                        onLoad:function(){
                            d2.datagrid('fixDetailRowHeight',index);
                        }
                    });
                    d2.datagrid('fixDetailRowHeight',index);
                },
                
                onDblClickRow:function(i,row){
                	var d=d2.datagrid('getRows')[i];
                	$("#ticket-tabs").tabs("add",{
                		title:d.SUBJECT,
                		content:"<iframe src='/ticket/tail/"+d.UUID+"'width='100%' height='600px' scrolling='no'></iframe>"
                	})
                },
				onClickCell:function(i,filed,value){
					var e=window.event;
					if(filed=="STATUS"){
						var d=d2.datagrid('getRows')[i];
						$("#ticketRightClickMenu").menu("show",{
							left: e.pageX,
							top: e.pageY,
							onClick:function(item){
								var name=item.name,
									text=$(item.target).attr("mval");
								util.get("/ticket/upstatus/"+d.UUID+"/"+text,function(){
									d2.datagrid("reload");
								})
							}
						})
					}
					return false;
				},
				columns:[[
			        {field:'UUID',title:'ID',width:"10%",hidden:true},
			        {field:'TICKET_ID',title:'工单编号',sortable:true,width:"20%"},
			        {field:'AUTHOR',title:'发起人',width:"10%"},
			        {field:'CUSTOMER',title:'工单客户',width:"10%"},
			        {field:'JOBID',title:'处理人',width:"10%"},
			        {field:'LEVEL',title:'优先等级',width:"8%",sortable:true,formatter:util.levelStatus,align:"center"},
			        {field:'BUSINESS',title:'业务类型',width:"10%"},
			        {field:'SUBJECT',title:'主题',width:"12%"},
			        {field:'CREATE_TIME',title:'创建时间',width:"12%",sortable:true,formatter:function(val){
			        	return util.dateFormat(val,"M/d h:m");
			        }},
			        {field:'STATUS',title:'状态',width:"8%",sortable:true,formatter:util.ticketStatus}
			    ]]
			}).datagrid('enableCellEditing').datagrid('gotoCell', {
				index: 0,
				field: 'UUID'
			});
			
			form.find("#ticketId").val("#"+jobId+util.dateFormat(new Date(),"yMdhms"));
			form.find("#author").val(jobId);
			
			View.initEvent();
		},
		
		initEvent:function(){
			
			$("#commit").on("click",function(){
				$("#commit").hide();
				//var tip=$("<label>正在提交、请稍等...</label>").appendTo($("#commit").parent());
				var data={};
				$("#ticket-form input,#ticket-form select,#ticket-form textarea").each(function(){
					data[$(this).attr("name")]=$(this).val();
				})
				util.post("/ticket/create",data,function(res){
					if(res.code==0){
						util.toast("创建成功");
					}else{
						util.toast("创建失败","error");
					}
					$("#commit").show();
				})
			})
			
			MySck.on("ticketTip",function(data){
				util.toast(data.content);
				var e=_icon.find(".edge");
				if(e.length==0){
					e=$('<div class="edge"></div>');
				}
				_icon.append(e);
				e.show();
			})
		}
	}

	function openWin() {
		var $div = $("<div/>").appendTo($("body"));
		$div.window({
			width: 900,
			height: 500,
			title: "工单系统",
			href: "plugs/ticket/template.html",
			minimizable: false,
			collapsible: false,
			shadow:false,
			onLoad: function() {
				var _this = $(this);
				View.initView(_this);
			},
			onClose: function() {
				$div.window("destroy", true);
			}
		})
	}

	return {

		init: function(icon,config) {
			_icon=icon;
			_PluginObject=config;
		},

		run: function() {
			_icon.find(".edge").hide();
			openWin();
		}

	}

})